(function () {
	//下拉框取值
	 var typeUrl = window.rootPath+ '/ui/partsType/getPartsType';
	 jQuery.ajax({
		 dataType: 'json',
         type: 'get',
         cache: false,
         url: encodeURI(typeUrl),
         contentType: 'application/json;charset=utf-8',
         success: function (data) {
        	 for(var i=0;i<data.data.length;i++){
        		 document.getElementById("partsType").innerHTML += 
        			 "<option value='"+data.data[i].id+"'>"+ data.data[i].parts_type + data.data[i].parts_name +"</option>";
        		 document.getElementById("partsWarnTypeSelect").innerHTML += 
        			 "<option value='"+data.data[i].id+"'>"+ data.data[i].parts_type + data.data[i].parts_name +"</option>";
        	 } 
         }
     });
	
	 
	//打开弹窗
	jQuery('.addBtn').on('click', function () {
		jQuery('#id').val('');
		jQuery('#warnMinValue').val('');
		jQuery('#warnMaxValue').val('');
		jQuery('#warnStatusInput').val('');
		jQuery('#warnExpInput').val('');
		var typeId  = jQuery('#partsType').val();
		var attributesUrl = window.rootPath+ '/ui/partsAttributes/getAttributesByTypeid?id='+ typeId;
		jQuery.ajax({
			dataType: 'json',
			type: 'get',
			cache: false,
			url: encodeURI(attributesUrl),
			contentType: 'application/json;charset=utf-8',
			success: function (data) {
				jQuery('#partsAttributes').empty(); 
				for(var i=0;i<data.data.length;i++){
					document.getElementById("partsAttributes").innerHTML += 
						"<option value='"+data.data[i].id+"'>"+ data.data[i].attributes + data.data[i].attributes_name +"</option>";
				} 
			}
		});
		jQuery('#partsWarnModal').modal('show');
	});

    ///编辑和新建保存
    jQuery('.saveBtn').on('click', function () {
    	if (jQuery("#partsWarnEditForm") && !jQuery("#partsWarnEditForm").Validform({
			tiptype : 3,
			showAllError : true
		}).check(false)) {
			return;
		};
		    var id = jQuery('#id').val();
		    var createTime = jQuery('#createTime').val();
		    var partsAttributes_id = jQuery('#partsAttributes').val();
		    var warnMinValue = jQuery('#warnMinValue').val();
		    var warnMinValueType = jQuery('#warnMinValueType').val();
		    var warnMaxValue = jQuery('#warnMaxValue').val();
		    var warnMaxValueType = jQuery('#warnMaxValueType').val();
		    var warnStatus = jQuery('#warnStatusInput').val();
		    var warnExp = jQuery('#warnExpInput').val();
	        var url = window.rootPath + "/ui/partsWarn/save";
	        var d;
	        if(id==''){
	        	  d = {
	        			"warnMinValue": warnMinValue,
	     	            "warnMinValueType": warnMinValueType,
	     	            "warnMaxValue":warnMaxValue,
	     	            "warnMaxValueType":warnMaxValueType,
	     	            "warnStatus":warnStatus,
	     	            "warnExp":warnExp,
	     	            "partsAttributes": 
	     	            {
	     	            	"id":partsAttributes_id
	     	            }
	     	        };
	        }else{
	        	d = {
	     	            "id":id,
	     	            "createTime":createTime,
	     	            "warnMinValue": warnMinValue,
	     	            "warnMinValueType": warnMinValueType,
	     	            "warnMaxValue":warnMaxValue,
	     	            "warnMaxValueType":warnMaxValueType,
	     	            "warnStatus":warnStatus,
	     	            "warnExp":warnExp,
	     	            "partsAttributes": 
	     	            {
	     	            	"id":partsAttributes_id
	     	            }
	     	        };
	        	
	        }
			if(warnMinValue==''&&warnMaxValue==''){
				alert("最小值和最大值不能同时为空，请输入！");
			}else if(warnMinValue!=''&&warnMaxValue!=''&&warnMinValue>warnMaxValue){
				alert("最小值不能大于最大值，请重新输入！");
			}else if(warnMinValue==warnMaxValue&&!(warnMinValueType==2&&warnMaxValueType==2)){
				alert("最小值和最大值不能相等，如需设置‘=’符号请把两个符号都设置为‘<=’");
			}
			else{
				jQuery.ajax({
					type: 'post',
					url: encodeURI(url),
					data: JSON.stringify(d),
					dataType: 'json',
					contentType: 'application/json;charset=utf-8',
					success: function (data) {
						jQuery('#partsWarnModal').modal('hide');
						serverTable.draw(false);
					}
				});
			}
    });

    var serverData = {};
    var serverTable;
    jQuery(document).ready(function () {
        serverTable = jQuery('#dataTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url":window.rootPath+ '/ui/partsWarn/getpage',
                "data": function (d) {
                    return $.extend({}, d, serverData);
                }
            },
            columns: [
                {
                data: null
                },
                
                {
                    data: 'partsAttributes.partsType.parts_name'
                },
                {
                    data: 'partsAttributes.attributes_name'
                },
                {
                	data: 'partsAttributes.attributes'
                },
                {
                	data: 'warnStatus'
                },
                {
                	data: 'warnExp'
                },
                {
                	 data: null,
                     defaultContent: "",
                     render: function (data, type, full, meta) {
                    	 var minValueType;
                    	 var maxValueType;
                    	 if(data.warnMinValueType == 1 ) minValueType = '<' 
                    		 else if(data.warnMinValueType == 2 ) minValueType = '<='
                         if(data.warnMaxValueType == 3 ) maxValueType = '<' 
                        	 else if(data.warnMaxValueType == 4 ) maxValueType = '<='
                        
                         if(data.warnMinValue==data.warnMaxValue&&data.warnMinValueType == 2&&data.warnMinValueType == 2){
                        	 var operate = '报警点' + ' = ' + data.warnMaxValue;
                        	}	 
                          else if(data.warnMinValue!=null&&data.warnMaxValue!=null){
                        	 var operate = data.warnMinValue +' '+ minValueType +' '+ '报警点' +' '+ maxValueType +' '+ data.warnMaxValue;
                         }else if(data.warnMinValue!=null){
                        	 var operate = data.warnMinValue +' '+ minValueType +' '+ '报警点';
                         }else if(data.warnMaxValue!=null){
                        	 var operate = '报警点' +' '+ maxValueType +' '+ data.warnMaxValue;
                         }
                         return operate;

                     }
                },
                {
                    data: null,
                    defaultContent: "",
                    render: function (data, type, full, meta) {
                        var operate ='<button type="button" class="btn btn-sm btn-default editBtn" >编辑</button>'
                            +'<button type="button" class="btn btn-sm btn-default delBtn" >删除</button>';
                        return operate;

                    }
                }
              ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }

                serverTable.off('click', 'tr');
                serverTable.on('click', 'tr', function (event) {
                    event = event || window.event;
                    var target = event.target || event.scrElement;
                    var data = serverTable.row(this).data();
                    if (target.className.indexOf('fa-remove') != -1 || target.className.indexOf('delBtn') != -1) {
                        deletePartsWarn(data);
                    }
                    if (target.className.indexOf('fa-pencil') != -1 || target.className.indexOf('editBtn') != -1) {
                        editPartsWarn(data);
                    }
                    return;
                });
            }
        });
        var deletePartsWarn = function (data) {
            if (!data) return;
            $.springLemonDialog({
                content: '确认删除' +data.partsAttributes.partsType.parts_type+data.partsAttributes.partsType.parts_name+'的'+'参数名为'+data.partsAttributes.attributes+'的报警类型？',//确认文本
                clickButton: function (sender, modal, index) {//按钮事件处理
                    //index:0 确认    1：取消
                    //sender:事件源 如按钮，sender.html()可得到按钮文字如：取消
                    if (index == 0) {
                        var url =window.rootPath+ "/api/partsWarns/" + data.id;
                        jQuery.ajax({
                            url: encodeURI(url),
                            cache: false,
                            type: "delete",
                            dataType: "json",
                            success: function (data) {
                                serverTable.draw(false);
                            }
                        });
                    }
                    $(this).closeDialog(modal);
                }

            });
        };
        var editPartsWarn=function(data){
        	if(!data) return;
        	jQuery('#id').val(data.id);
        	jQuery('#createTime').val(data.createTime)
        	jQuery('#warnMinValue').val(data.warnMinValue);
		    jQuery('#warnMinValueType').val(data.warnMinValueType);
		    jQuery('#warnMaxValue').val(data.warnMaxValue);
		    jQuery('#warnMaxValueType').val(data.warnMaxValueType);
		    jQuery('#warnStatusInput').val(data.warnStatus);
		    jQuery('#warnExpInput').val(data.warnExp);
        	jQuery("#partsType").val(data.partsAttributes.partsType.id);
//        	jQuery("#warnValueType").select2().val(data.warnValueType).trigger("change");
//        	jQuery("#partsType").select2().val(data.partsAttributes.partsType.id).trigger("change");
        	var attributesId = data.partsAttributes.id;
        	
        	var typeId  = jQuery('#partsType').val();
    		var attributesUrl = window.rootPath+ '/ui/partsAttributes/getAttributesByTypeid?id='+ typeId;
    		jQuery.ajax({
    			async:false,
    			dataType: 'json',
    			type: 'get',
    			cache: false,
    			url: encodeURI(attributesUrl),
    			contentType: 'application/json;charset=utf-8',
    			success: function (data) {
    				jQuery('#partsAttributes').empty(); 
    				for(var i=0;i<data.data.length;i++){
    						document.getElementById("partsAttributes").innerHTML += 
    							"<option value='"+data.data[i].id+"'>"+ data.data[i].attributes + data.data[i].attributes_name +"</option>";
    				} 
    				jQuery('#partsAttributes').val(attributesId);
    			}
    		});
//    		jQuery("#partsAttributes").select2().val(data.partsAttributes.id).trigger("change");
    		jQuery('#partsWarnModal').modal('show');
        }
    });
    //查询
    jQuery('.searchBtn').on('click', function() {
    	serverData.id = jQuery('#partsWarnTypeSelect').val();
    	serverData.key = jQuery('#keyword').val();
		serverTable.draw();
	})
	jQuery('#partsWarnTypeSelect').change(function() {
		serverData.id = jQuery('#partsWarnTypeSelect').val();
		serverData.key = '';
		serverTable.draw();
	})
	// 撤销查询
	jQuery('.resetBtn').on("click", function() {
		jQuery('#keyword').val('');
		jQuery('#partsWarnTypeSelect').val('');
		serverData.id = '';
		serverData.key = '';
		serverTable.draw();
	});
    // 下拉框变更
    jQuery('#partsType').change(function() {
    	var typeId  = jQuery('#partsType').val();
		var attributesUrl = window.rootPath+ '/ui/partsAttributes/getAttributesByTypeid?id='+ typeId;
		jQuery.ajax({
			dataType: 'json',
			type: 'get',
			cache: false,
			url: encodeURI(attributesUrl),
			contentType: 'application/json;charset=utf-8',
			success: function (data) {
				jQuery('#partsAttributes').empty(); 
				for(var i=0;i<data.data.length;i++){
					document.getElementById("partsAttributes").innerHTML += 
						"<option value='"+data.data[i].id+"'>"+ data.data[i].attributes + data.data[i].attributes_name +"</option>";
				} 
			}
		});
    });
    
})();